# coding:utf-8
# writingtime: 2022-9-2
# author: wanjun

from Utilities.AutoGetOperator.selectPackage import get_func
from Data.paperData import PaperData


class Polymerization:
    def __init__(self, q=None, decision_group=None, operator=None, decision_method=None, score_function=None,
                 distance_function=None, get_expert_function=None, x=2, a=2, b=2):
        """

        :param decision_group:
        :param q:
        :param operator:
        :param decision_method:
        :param score_function:
        :param distance_function:
        :param x:
        :param a:
        :param b:
        """
        self.q = 9 if q is None else q
        self.decision_group = PaperData().daili() if decision_group is None else decision_group
        self.decision_method = get_func(r'DecisionMethod\MABAC.py',
                                        'MABAC') if decision_method is None else decision_method
        self.operator = get_func(r'Operators\OperationOperators\Algebraic.py', 'WA') if operator is None else operator
        self.score_function = get_func(r'ScoreFunction\getS.py', 'getS') if score_function is None else score_function
        self.distance_function = get_func(r'Measure/distance/getD.py',
                                          'getD') if distance_function is None else distance_function
        self.get_expertweight = get_func(r'Utilities\GetWeight\getExpertWeight\GetSatisfactionWeight.py',
                                         'getSatisfactionWeight') if get_expert_function is None else get_expert_function
        self.x = x
        self.a = a
        self.b = b

    def matrixReverse(self, matrix):
        '''
        function: 功能函数
        :param matrix: 矩阵
        :return: 行列交换的矩阵
        '''
        newMatrix = []
        for x in range(len(matrix[0])):
            temp = []
            for y in range(len(matrix)):
                temp.append(matrix[y][x])
            newMatrix.append(temp)

        return newMatrix

    def getresult(self):
        aggre_matrix_1 = []  # 存放运算结果
        expert_weight = self.get_expertweight(self.decision_group, q=self.q).getResult()
        '''集结矩阵'''
        for i in range(len(self.decision_group[0])):
            li_1 = []
            for j in range(len(self.decision_group[0][0])):
                li_1.append(
                    self.operator([self.decision_group[k][i][j] for k in range(len(self.decision_group))],
                                  expert_weight, q=self.q, x=self.x, a=self.a, b=self.b).getResult())
            aggre_matrix_1.append(li_1)
        """此处使用决策方法计算属性的评分，因为默认的决策方法无法计算权重，因此此处将矩阵转置，计算属性的得分占比作为属性属性权重"""

        reverseamtrix = self.matrixReverse(aggre_matrix_1)
        attr_weight = self.decision_method(reverseamtrix,
                                                [1 / len(reverseamtrix[0]) for i in range(len(reverseamtrix[0]))],
                                                self.q).getResult()
        # 最终结果
        fianl_vlaue = [self.operator(i, attr_weight, q=self.q, x=self.x, a=self.a, b=self.b).getResult() for i in
                       aggre_matrix_1]

        return fianl_vlaue


if __name__ == "__main__":
    ex = Polymerization()
    print(ex.getresult())
